package com.zeh.service.Impl;

import com.zeh.mapper.BlogMapper;
import com.zeh.mapper.CommentMapper;
import com.zeh.pojo.Comment;
import com.zeh.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 博客评论业务层接口实现类
 * @Author: zeh
 */
@Service
public class CommentServiceImpl  implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private BlogMapper blogMapper;

    //存放迭代找出的所有子代的集合
    private List<Comment> tempReplys = new ArrayList<>();

    /**
     * @Description: 查询评论
     * @Auther: zeh
     * @Param: blogId：博客id
     * @Return: 评论消息
     */
    @Override
    public List<Comment> listCommentByBlogId(Long blogId) {
        //查询出父节点
        List<Comment> comments = commentMapper.findByBlogIdParentIdNull(blogId, Long.parseLong("-1"));
        for(Comment comment : comments){
            Long id = comment.getId();
            String nickname = comment.getNickname();
            List<Comment> childComments = commentMapper.findByBlogIdParentIdNull(blogId, id);

            //查询出子评论
            combineChildren(blogId,childComments,nickname);
            comment.setReplyComments(tempReplys);
            tempReplys = new ArrayList<>();
        }
        return comments;
    }


    /**
     * @Description: 查询出子评论
     * @Auther: zeh
     * @Param: childComments：所有子评论
     * @Param: parentNickname1：父评论姓名
     * @Return:
     */
    private void combineChildren(Long blogId,List<Comment> childComments,String parentNickname1){
        //判断是否有一级子评论
        if(childComments.size()>0){
            //循环找出子评论的id
            for (Comment childComment : childComments){
                String parentNickname = childComment.getNickname();
                childComment.setParentNickname(parentNickname1);
                tempReplys.add(childComment);
                Long childId = childComment.getId();
                //查询出子二级评论
                recursively(blogId,childId,parentNickname);
            }

        }
    }


    private void recursively(Long blogId,Long childId,String parentNickname1){
        //根据子一级评论的id找到子二级评论
        List<Comment> replayComments = commentMapper.findByBlogIdAndReplayId(blogId, childId);

        if(replayComments.size()>0){
            for(Comment replayComment : replayComments){
                String parentNickname = replayComment.getNickname();
                replayComment.setParentNickname(parentNickname1);
                Long replayId = replayComment.getId();
                tempReplys.add(replayComment);
                recursively(blogId,replayId,parentNickname);
            }
        }
    }

    //新增评论
    @Override
    public int saveComment(Comment comment) {
        comment.setCreateTime(new Date());
        int comments = commentMapper.saveComment(comment);
        //文章评论统计数
        blogMapper.getCommentCountById(comment.getBlogId());
        return comments;
    }

    @Override
    public void deleteComment(Comment comment, Long id) {
       commentMapper.deleteById(id);
       blogMapper.getCommentCountById(comment.getBlogId());
    }

}
